<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use Illuminate\Support\Facades\Hash;
use View;
use App\Model\Member;

class MemberController extends AdminBaseController
{
    public function __construct()
    {
        $option = ['module_name'=>'member','status'=>'member_status'];
        $this->setModelObj(new Member,$option);
    }

    /**
     * 获取正确的排序组合
     */
    protected function getTrueOrder($data)
    {
        $orderby = [];
        if(!array_key_exists('sort',$data)){
            $orderby['field'] = $this->model_keyname;
            $orderby['type'] = 'desc';
        }else{
            switch ($data['sort']){
                case 'add_time_format':
                    $orderby['field'] = 'add_time';break;
                default:
                    $orderby['field'] = $data['sort'];break;
            }
            $orderby['type'] =  $data['order'];
        }
        return $orderby;
    }

	/**
	 * 后台会员编辑保存
	 */
	public function toMemberEdit(Requests\MemberEditRequest $request, $memberid)
	{
		$data = $request->except(['_token','_method']);
		if($request->get('member_password')){
			$data['member_password'] = Hash::make(md5($data['member_password']));
		}
		$data['member_edit_time'] = time();
		$data['member_edit_ip'] = $request->getClientIp();
		$re = Member::where('member_id',$memberid)->update($data);
        $jsonData = $re ? $this->getJsonData(2,'编辑成功') : $this->getJsonData(0,'编辑失败');
		return response()->json($jsonData);
	}
}